<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>辅助工具/函数 | BuildAdmin</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/images/favicon.ico">
    <meta name="description" content="使用流行技术栈快速创建商业级后台管理系统-BuildAdmin是基于TP6、Vue3.x、Typescript、Vite、Pinia、Element plus等的开源后台系统">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="keywords" content="BuildAdmin,ThinkPHP6通用后台,Vue3后台,BuildAdmin文档,vue3开源后台,vue3模板与案例市场">
    <meta name="author" content="妙码生花">
    <meta name="robots" content="all,follow">
    
    <link rel="preload" href="/assets/css/0.styles.76db4400.css" as="style"><link rel="preload" href="/assets/js/app.26dfc26f.js" as="script"><link rel="preload" href="/assets/js/2.845fc4bd.js" as="script"><link rel="preload" href="/assets/js/80.a64f0503.js" as="script"><link rel="prefetch" href="/assets/js/10.31502a42.js"><link rel="prefetch" href="/assets/js/11.c0667200.js"><link rel="prefetch" href="/assets/js/12.0022daab.js"><link rel="prefetch" href="/assets/js/13.496b9007.js"><link rel="prefetch" href="/assets/js/14.57a77bc0.js"><link rel="prefetch" href="/assets/js/15.a2ee4fc7.js"><link rel="prefetch" href="/assets/js/16.7050523a.js"><link rel="prefetch" href="/assets/js/17.c4c5529f.js"><link rel="prefetch" href="/assets/js/18.f0350247.js"><link rel="prefetch" href="/assets/js/19.60586b14.js"><link rel="prefetch" href="/assets/js/20.4c6361b4.js"><link rel="prefetch" href="/assets/js/21.f793d386.js"><link rel="prefetch" href="/assets/js/22.299fa3b0.js"><link rel="prefetch" href="/assets/js/23.460af9b1.js"><link rel="prefetch" href="/assets/js/24.67771dfc.js"><link rel="prefetch" href="/assets/js/25.57b0bfa7.js"><link rel="prefetch" href="/assets/js/26.30e9faee.js"><link rel="prefetch" href="/assets/js/27.15ffda62.js"><link rel="prefetch" href="/assets/js/28.4ca46604.js"><link rel="prefetch" href="/assets/js/29.09a58cb6.js"><link rel="prefetch" href="/assets/js/3.959fd4f8.js"><link rel="prefetch" href="/assets/js/30.c53f15cb.js"><link rel="prefetch" href="/assets/js/31.0d30028e.js"><link rel="prefetch" href="/assets/js/32.4591b41b.js"><link rel="prefetch" href="/assets/js/33.bd9ed146.js"><link rel="prefetch" href="/assets/js/34.0bedaafb.js"><link rel="prefetch" href="/assets/js/35.dded2c4a.js"><link rel="prefetch" href="/assets/js/36.3f14f7ef.js"><link rel="prefetch" href="/assets/js/37.348ab3dc.js"><link rel="prefetch" href="/assets/js/38.bea67b35.js"><link rel="prefetch" href="/assets/js/39.66735bad.js"><link rel="prefetch" href="/assets/js/4.a7dad9f3.js"><link rel="prefetch" href="/assets/js/40.24ae817d.js"><link rel="prefetch" href="/assets/js/41.20484420.js"><link rel="prefetch" href="/assets/js/42.d2d9cf07.js"><link rel="prefetch" href="/assets/js/43.638926f9.js"><link rel="prefetch" href="/assets/js/44.5b2e8865.js"><link rel="prefetch" href="/assets/js/45.b08599e3.js"><link rel="prefetch" href="/assets/js/46.3e165b3e.js"><link rel="prefetch" href="/assets/js/47.0f981d94.js"><link rel="prefetch" href="/assets/js/48.52c21e5e.js"><link rel="prefetch" href="/assets/js/49.5d5d4c94.js"><link rel="prefetch" href="/assets/js/5.2d6b30a8.js"><link rel="prefetch" href="/assets/js/50.379ba4f0.js"><link rel="prefetch" href="/assets/js/51.4f4ecaea.js"><link rel="prefetch" href="/assets/js/52.81ccb047.js"><link rel="prefetch" href="/assets/js/53.419686cb.js"><link rel="prefetch" href="/assets/js/54.783cb319.js"><link rel="prefetch" href="/assets/js/55.110fde12.js"><link rel="prefetch" href="/assets/js/56.53900e45.js"><link rel="prefetch" href="/assets/js/57.55cd3551.js"><link rel="prefetch" href="/assets/js/58.2b82c178.js"><link rel="prefetch" href="/assets/js/59.fec4fe12.js"><link rel="prefetch" href="/assets/js/6.4e9a5a2e.js"><link rel="prefetch" href="/assets/js/60.99354f6f.js"><link rel="prefetch" href="/assets/js/61.c528c138.js"><link rel="prefetch" href="/assets/js/62.831c810e.js"><link rel="prefetch" href="/assets/js/63.9923d5c6.js"><link rel="prefetch" href="/assets/js/64.01b9c0fd.js"><link rel="prefetch" href="/assets/js/65.1288d968.js"><link rel="prefetch" href="/assets/js/66.f7c8aabd.js"><link rel="prefetch" href="/assets/js/67.89455afa.js"><link rel="prefetch" href="/assets/js/68.ca185a9e.js"><link rel="prefetch" href="/assets/js/69.dfe2288f.js"><link rel="prefetch" href="/assets/js/7.58476ce6.js"><link rel="prefetch" href="/assets/js/70.ea823c37.js"><link rel="prefetch" href="/assets/js/71.9e222b70.js"><link rel="prefetch" href="/assets/js/72.77b15e17.js"><link rel="prefetch" href="/assets/js/73.f42962da.js"><link rel="prefetch" href="/assets/js/74.a2912f27.js"><link rel="prefetch" href="/assets/js/75.8db3af79.js"><link rel="prefetch" href="/assets/js/76.45426b7e.js"><link rel="prefetch" href="/assets/js/77.a5d8ef7a.js"><link rel="prefetch" href="/assets/js/78.cfcdd83a.js"><link rel="prefetch" href="/assets/js/79.d8b5e327.js"><link rel="prefetch" href="/assets/js/8.419e773f.js"><link rel="prefetch" href="/assets/js/9.633ba216.js">
    <link rel="stylesheet" href="/assets/css/0.styles.76db4400.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container max-content"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/logo.png" alt="BuildAdmin" class="logo"> <span class="site-name can-hide">BuildAdmin</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/senior/" class="nav-link router-link-active">
  进阶
</a></div><div class="nav-item"><a href="https://buildadmin.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  官网
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://demo.buildadmin.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  线上演示
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="代码仓库" class="dropdown-title"><span class="title">代码仓库</span> <span class="arrow down"></span></button> <button type="button" aria-label="代码仓库" class="mobile-dropdown-title"><span class="title">代码仓库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/wonderful-code/buildadmin" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/build-admin/buildadmin" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><a href="/fastadmin.html" class="nav-link">
  🎉FastAdmin
</a></div><div class="nav-item"><a href="https://gitee.com/wonderful-code/buildadmin/blob/master/CHANGELOG.md" target="_blank" rel="noopener noreferrer" class="nav-link external">
  更新日志
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <a href="https://gitee.com/wonderful-code/build-admin-doc" target="_blank" rel="noopener noreferrer" class="repo-link">
    文档仓库
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/senior/" class="nav-link router-link-active">
  进阶
</a></div><div class="nav-item"><a href="https://buildadmin.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  官网
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://demo.buildadmin.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  线上演示
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="代码仓库" class="dropdown-title"><span class="title">代码仓库</span> <span class="arrow down"></span></button> <button type="button" aria-label="代码仓库" class="mobile-dropdown-title"><span class="title">代码仓库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/wonderful-code/buildadmin" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/build-admin/buildadmin" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><a href="/fastadmin.html" class="nav-link">
  🎉FastAdmin
</a></div><div class="nav-item"><a href="https://gitee.com/wonderful-code/buildadmin/blob/master/CHANGELOG.md" target="_blank" rel="noopener noreferrer" class="nav-link external">
  更新日志
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <a href="https://gitee.com/wonderful-code/build-admin-doc" target="_blank" rel="noopener noreferrer" class="repo-link">
    文档仓库
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>起步</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/senior/" aria-current="page" class="sidebar-link">后台交互</a></li><li><a href="/senior/directoryStructure.html" class="sidebar-link">目录结构</a></li><li><a href="/senior/oneClickCRUD.html" class="sidebar-link">CRUD代码生成</a></li><li><a href="/senior/databaseSpecification.html" class="sidebar-link">数据表设计规范</a></li><li><a href="/senior/multilingual.html" class="sidebar-link">国际化（多语言）</a></li><li><a href="/senior/addMenuRule.html" class="sidebar-link">菜单规则（路由与权限）</a></li><li><a href="/senior/faq.html" class="sidebar-link">常见问题</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>WEB端专项</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/senior/web/icon.html" class="sidebar-link">字体图标</a></li><li><a href="/senior/web/axios.html" class="sidebar-link">网络请求</a></li><li><a href="/senior/web/stores.html" class="sidebar-link">状态管理</a></li><li><a href="/senior/web/formItem.html" class="sidebar-link">表单项目组件（formItem）</a></li><li><a href="/senior/web/baInput.html" class="sidebar-link">输入组件（baInput）</a></li><li><a href="/senior/web/baTable.html" class="sidebar-link">表格组件（table）</a></li><li><a href="/senior/web/terminal.html" class="sidebar-link">WEB终端组件</a></li><li><a href="/senior/web/formValidation.html" class="sidebar-link">表单验证</a></li><li><a href="/senior/web/directives.html" class="sidebar-link">内置指令</a></li><li><a href="/senior/web/utils.html" aria-current="page" class="active sidebar-link">辅助工具/函数</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/senior/web/utils.html#加载网络css-js文件" class="sidebar-link">加载网络CSS/JS文件</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#设置页面标题" class="sidebar-link">设置页面标题</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#检查url是否是外部的" class="sidebar-link">检查URL是否是外部的</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#获取管理员身份令牌" class="sidebar-link">获取管理员身份令牌</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#获取会员身份令牌" class="sidebar-link">获取会员身份令牌</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#防抖" class="sidebar-link">防抖</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#表单重置" class="sidebar-link">表单重置</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#是否在后台应用内" class="sidebar-link">是否在后台应用内</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#是否为手机设备" class="sidebar-link">是否为手机设备</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#按钮鉴权-函数式" class="sidebar-link">按钮鉴权（函数式）</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#随机数生成" class="sidebar-link">随机数生成</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#切换路由" class="sidebar-link">切换路由</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#本地缓存与会话缓存" class="sidebar-link">本地缓存与会话缓存</a></li><li class="sidebar-sub-header"><a href="/senior/web/utils.html#快速获取当前应用实例" class="sidebar-link">快速获取当前应用实例</a></li></ul></li><li><a href="/senior/web/styles.html" class="sidebar-link">CSS/SCSS样式</a></li><li><a href="/senior/web/justNeedWeb.html" class="sidebar-link">只需要WEB端</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>WebNuxt专项（SSR）</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/senior/nuxt/intro.html" class="sidebar-link">介绍</a></li><li><a href="/senior/nuxt/directoryStructure.html" class="sidebar-link">目录结构</a></li><li><a href="/senior/nuxt/request.html" class="sidebar-link">网络请求</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Server端专项</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/senior/server/debug.html" class="sidebar-link">开启调试</a></li><li><a href="/senior/server/apiDebug.html" class="sidebar-link">调试接口</a></li><li><a href="/senior/server/config.html" class="sidebar-link">配置</a></li><li><a href="/senior/server/controller.html" class="sidebar-link">控制器</a></li><li><a href="/senior/server/captcha.html" class="sidebar-link">验证码</a></li><li><a href="/senior/server/dataLImit.html" class="sidebar-link">数据权限控制</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>模块开发</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/senior/module/start.html" class="sidebar-link">开始</a></li><li><a href="/senior/module/basicInfo.html" class="sidebar-link">模块基本信息</a></li><li><a href="/senior/module/directoryStructure.html" class="sidebar-link">模块目录结构</a></li><li><a href="/senior/module/configJson.html" class="sidebar-link">模块基本配置</a></li><li><a href="/senior/module/installSql.html" class="sidebar-link">模块安装SQL</a></li><li><a href="/senior/module/coreController.html" class="sidebar-link">模块核心控制器</a></li><li><a href="/senior/module/event.html" class="sidebar-link">模块行为事件</a></li><li><a href="/senior/module/webBootstrap.html" class="sidebar-link">向 main.ts 插入代码</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="辅助工具-函数"><a href="#辅助工具-函数" class="header-anchor">#</a> 辅助工具/函数</h1> <h2 id="加载网络css-js文件"><a href="#加载网络css-js文件" class="header-anchor">#</a> 加载网络CSS/JS文件</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> loadCss<span class="token punctuation">,</span> loadJs <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/common'</span>
<span class="token function">loadCss</span><span class="token punctuation">(</span><span class="token string">'网络css文件url'</span><span class="token punctuation">)</span>
<span class="token function">loadJs</span><span class="token punctuation">(</span><span class="token string">'网络js文件url'</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="设置页面标题"><a href="#设置页面标题" class="header-anchor">#</a> 设置页面标题</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> setTitle <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/common'</span>
<span class="token function">setTitle</span><span class="token punctuation">(</span><span class="token string">'新的页面标题'</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="检查url是否是外部的"><a href="#检查url是否是外部的" class="header-anchor">#</a> 检查URL是否是外部的</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> isExternal <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/common'</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isExternal</span><span class="token punctuation">(</span><span class="token string">'路径'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'是外部链接'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'不是外部链接'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="获取管理员身份令牌"><a href="#获取管理员身份令牌" class="header-anchor">#</a> 获取管理员身份令牌</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> useAdminInfo <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/stores/adminInfo'</span>
<span class="token keyword">const</span> adminInfo <span class="token operator">=</span> <span class="token function">useAdminInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>adminInfo<span class="token punctuation">.</span><span class="token function">getToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>adminInfo<span class="token punctuation">.</span><span class="token function">getToken</span><span class="token punctuation">(</span><span class="token string">'refresh'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// token 刷新令牌</span>
</code></pre></div><h2 id="获取会员身份令牌"><a href="#获取会员身份令牌" class="header-anchor">#</a> 获取会员身份令牌</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> useUserInfo <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/stores/userInfo'</span>
<span class="token keyword">const</span> userInfo <span class="token operator">=</span> <span class="token function">useUserInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>userInfo<span class="token punctuation">.</span><span class="token function">getToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>userInfo<span class="token punctuation">.</span><span class="token function">getToken</span><span class="token punctuation">(</span><span class="token string">'refresh'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// token 刷新令牌</span>
</code></pre></div><h2 id="防抖"><a href="#防抖" class="header-anchor">#</a> 防抖</h2> <p><code>debounce</code>函数，在<code>间隔毫秒数内</code>重复触发<code>执行函数</code>，则只执行一次</p> <ul><li>参数一：执行函数</li> <li>参数二：间隔毫秒数</li></ul> <div class="language-vue extra-class"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-input</span> <span class="token attr-name">v-model</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>state.search<span class="token punctuation">&quot;</span></span> <span class="token attr-name">@input</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>debounce(onSearchInput, 500)()<span class="token punctuation">&quot;</span></span> <span class="token punctuation">/&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">import</span> <span class="token punctuation">{</span> reactive <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> debounce <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/common'</span>

<span class="token keyword">const</span> state <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    <span class="token literal-property property">search</span><span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> <span class="token function-variable function">onSearchInput</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'开始搜索:'</span> <span class="token operator">+</span> state<span class="token punctuation">.</span>search<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><h2 id="表单重置"><a href="#表单重置" class="header-anchor">#</a> 表单重置</h2> <div class="language-vue extra-class"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-form</span> <span class="token attr-name">ref</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>formRef<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>onResetForm(formRef)<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>重置<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-button</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-form</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">import</span> <span class="token punctuation">{</span> onResetForm <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/common'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> ElForm <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'element-plus'</span>

<span class="token keyword">const</span> formRef <span class="token operator">=</span> ref<span class="token operator">&lt;</span>InstanceType<span class="token operator">&lt;</span><span class="token keyword">typeof</span> ElForm<span class="token operator">&gt;&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><h2 id="是否在后台应用内"><a href="#是否在后台应用内" class="header-anchor">#</a> 是否在后台应用内</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> isAdminApp <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/common'</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isAdminApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'在后台'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'不在后台'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="是否为手机设备"><a href="#是否为手机设备" class="header-anchor">#</a> 是否为手机设备</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> isMobile <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/common'</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isMobile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'用户使用手机访问'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="按钮鉴权-函数式"><a href="#按钮鉴权-函数式" class="header-anchor">#</a> 按钮鉴权（函数式）</h2> <p><code>auth</code>函数会以当前页面的<code>path</code>从服务端获取到的权限节点进行比对，来确定用户是否拥有某个按钮的权限。</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> auth <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/common'</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">auth</span><span class="token punctuation">(</span><span class="token string">'add'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'拥有添加权限'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'不拥有添加权限'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="随机数生成"><a href="#随机数生成" class="header-anchor">#</a> 随机数生成</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> randomNum<span class="token punctuation">,</span> uuid<span class="token punctuation">,</span> shortUuid <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/random'</span>
<span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">randomNum</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 生成4到6位的随机数</span>
<span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">shortUuid</span><span class="token punctuation">(</span><span class="token string">'前缀'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 生成唯一标识</span>
<span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">uuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 生成全球唯一标识</span>
</code></pre></div><h2 id="切换路由"><a href="#切换路由" class="header-anchor">#</a> 切换路由</h2> <p>通过此函数切换路由时，切换失败，会有错误提示，参数同<code>router.push</code></p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> routePush <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/router'</span>
<span class="token function">routePush</span><span class="token punctuation">(</span><span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">'routine/adminInfo'</span><span class="token punctuation">,</span> params<span class="token operator">:</span> <span class="token punctuation">{</span> a<span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token function">routePush</span><span class="token punctuation">(</span><span class="token string">'/admin/dashboard'</span><span class="token punctuation">)</span>

<span class="token function">routePush</span><span class="token punctuation">(</span><span class="token string">'/test/posva#hash'</span><span class="token punctuation">)</span>
<span class="token function">routePush</span><span class="token punctuation">(</span><span class="token punctuation">{</span> path<span class="token operator">:</span> <span class="token string">'/test/posva'</span><span class="token punctuation">,</span> hash<span class="token operator">:</span> <span class="token string">'#hash'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token function">routePush</span><span class="token punctuation">(</span><span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">'test'</span><span class="token punctuation">,</span> params<span class="token operator">:</span> <span class="token punctuation">{</span> a<span class="token operator">:</span> <span class="token string">'1'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> hash<span class="token operator">:</span> <span class="token string">'#hash'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">// 只改变 hash</span>
<span class="token function">routePush</span><span class="token punctuation">(</span><span class="token punctuation">{</span> hash<span class="token operator">:</span> <span class="token string">'#hash'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">// 只改变 query</span>
<span class="token function">routePush</span><span class="token punctuation">(</span><span class="token punctuation">{</span> query<span class="token operator">:</span> <span class="token punctuation">{</span> page<span class="token operator">:</span> <span class="token string">'2'</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">// 只改变 param</span>
<span class="token function">routePush</span><span class="token punctuation">(</span><span class="token punctuation">{</span> params<span class="token operator">:</span> <span class="token punctuation">{</span> a<span class="token operator">:</span> <span class="token string">'1'</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="本地缓存与会话缓存"><a href="#本地缓存与会话缓存" class="header-anchor">#</a> 本地缓存与会话缓存</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> Local<span class="token punctuation">,</span> Session <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'/@/utils/storage'</span>

Local<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">)</span> <span class="token comment">// 设置本地缓存</span>
Local<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">)</span> <span class="token comment">// 获取本地缓存值</span>
Local<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">)</span> <span class="token comment">// 删除某个本地缓存</span>
Local<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 清理所有本地缓存</span>

Session<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">)</span> <span class="token comment">// 设置会话缓存</span>
Session<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">)</span> <span class="token comment">// 获取会话缓存值</span>
Session<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">)</span> <span class="token comment">// 删除某个会话缓存</span>
Session<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 清理所有会话缓存</span>
</code></pre></div><h2 id="快速获取当前应用实例"><a href="#快速获取当前应用实例" class="header-anchor">#</a> 快速获取当前应用实例</h2> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> useCurrentInstance <span class="token keyword">from</span> <span class="token string">'/@/utils/useCurrentInstance'</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> proxy <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">useCurrentInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

proxy<span class="token punctuation">.</span>eventBus<span class="token punctuation">.</span><span class="token function">off</span><span class="token punctuation">(</span><span class="token string">'xxx'</span><span class="token punctuation">)</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://gitee.com/wonderful-code/build-admin-doc/edit/master/docs/senior/web/utils.md" target="_blank" rel="noopener noreferrer">点此帮助我们改善此页面</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/12/13 22:28:17</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/senior/web/directives.html" class="prev">
        内置指令
      </a></span> <span class="next"><a href="/senior/web/styles.html">
        CSS/SCSS样式
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.26dfc26f.js" defer></script><script src="/assets/js/2.845fc4bd.js" defer></script><script src="/assets/js/80.a64f0503.js" defer></script>
  </body>
</html>
